<template>
  <div>
    <div class="margin-container-list">
      <!--顶部搜索 -->
      <div class="top-operate">
        <div>
          <div class="float-l">
            <el-input @keyup.enter.native="handleSearch" v-model="searchKeyword" placeholder="请输入关键词" style="width: 300px"></el-input>
            <el-cascader v-model="categoryIds"  placeholder="选择商品分类" :options="categoryData" :props="{ checkStrictly: true }" change-on-select  class="default-input" filterable></el-cascader>
            <el-select v-model="searchLabelType" placeholder="标签" >
              <el-option v-for="(val, key) in dict.labelType" :key="key" :label="val" :value="key">
              </el-option>
            </el-select>

            <el-button type="primary" plain @click="handleSearch">查询</el-button>
          </div>
          <br>
        </div>
      </div>

      <!-- 内容列表 -->
      <div class="table-container">
        <el-table  ref="multipleTable" :data="goodsList" v-loading="listLoading"
                   element-loading-text="加载中" style="width: 100%" >
          <el-table-column min-width="350px" label="商品" prop="title">
            <template slot-scope="scope">
              <div @click="handleEdit(scope.row)" class="flex v-align-c cursor-p">
                <label><img :src="scope.row.mainImage" class="user-avatar"></label>
                <div class="font-14 pad-l-10">
                  <div>{{scope.row.title}}</div>
                  <template v-if="scope.row.categoryName">
                    <div class="category-style">[{{scope.row.categoryName}}]</div>
                  </template>
                </div>
              </div>
            </template>>
          </el-table-column>
          <el-table-column width="100px" label="零售价格" prop="sellPrice"></el-table-column>
          <el-table-column width="150px" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="handleUp(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="searchListQuery.currentPage"
                       :page-sizes="[10, 20, 50, 100]" :page-size="searchListQuery.size"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
  .user-avatar{
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
    border: #efefef 1px solid;
  }
  .category-style{
    color: #3abbdb
  }
</style>

<script>
  import { getGoodsList, listGoodsCategory } from '@/api/goods/goods'
  import { getDictByGroupName } from '@/api/common/common'
  import elDragDialog from '@/directive/el-dragDialog'

  export default {
    components: {},
    directives: {
      // 拖动窗口
      elDragDialog
    },
    props: {
      // details1: {
      //   type: Object,
      //   default: null
      // },
      eventId: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        total: null,
        multipleSelection: [],
        listLoading: true,
        goodsSortNo: null,
        searchListQuery: {
          enterpriseId: this.getEnterpriseId(),
          currentPage: 1,
          size: 10,
          title: '',
          goodsStatus: '1'
        },
        searchLabelType: '',
        searchKeyword: '',
        dict: {},
        isDict: false,
        categoryIds: [],
        goodsList: [],
        // 商品的所有分类json
        categoryData: [],
        goodsDetails: {
          componentGoodsId: '',
          detailsId: '',
          goodsId: '',
          goodsSortNo: 0,
          imgUrl: '',
          title: '',
          price: '',
          stock: ''
        }
      }
    },
    // 初始化页面
    created() {
      this.initDict()
      this.listGoodsCategory()
      this.initGoodsList()
    },
    filters: {
      priceFilter(e) {
        return e.toFixed(2)
      }
    },
    mounted() {
      const that = this
      this.$bus.$on('selectComponentGoods' + this.eventId, function(goodsSortNo) {
        that.goodsSortNo = goodsSortNo
      })
    },
    methods: {
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.initGoodsList()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.initGoodsList()
      },
      initDict() {
        getDictByGroupName('ProjectGoodsDTO').then(response => {
          this.dict = response.data
        })
      },
      initGoodsList() {
        if (this.searchLabelType) {
          this.searchListQuery['labelType'] = this.searchLabelType
        }
        this.searchListQuery['title'] = this.searchKeyword
        this.searchListQuery['oneCategoryId'] = ''
        this.searchListQuery['twoCategoryId'] = ''
        this.searchListQuery['threeCategoryId'] = ''
        const categoryIds = this.categoryIds
        if (categoryIds.length === 1) {
          this.searchListQuery['oneCategoryId'] = categoryIds[0]
        }
        if (categoryIds.length === 2) {
          this.searchListQuery['twoCategoryId'] = categoryIds[1]
        }
        if (categoryIds.length === 3) {
          this.searchListQuery['threeCategoryId'] = categoryIds[2]
        }
        this.listLoading = true
        // 搜索状态
        getGoodsList(this.searchListQuery).then(response => {
          this.total = response.total
          this.goodsList = response.records
          if (!this.isDict) {
            this.dict = response.dict
            this.isDict = true
          }
          this.listLoading = false
        })
      },
      // 获取类目
      listGoodsCategory() {
        const enterpriseId = this.getEnterpriseId()
        listGoodsCategory(enterpriseId).then(response => {
          this.categoryData = response.data
        })
      },
      handleSearch() {
        this.initGoodsList()
      },
      // 选择
      handleUp(row) {
        this.goodsDetails.goodsSortNo = this.goodsSortNo
        this.goodsDetails.imgUrl = row.mainImage
        this.goodsDetails.goodsId = row.goodsId
        this.goodsDetails.title = row.title
        this.goodsDetails.price = row.sellPrice
        this.goodsDetails.stock = row.stock
        this.$bus.$emit('choiceDetailsGoods' + this.eventId, this.goodsDetails)
      }
    }
  }

</script>
